<?php
require_once('../../config.php');
require_once '../utils.php';

Doctrine::loadModels('../../models');

	$agent_type_id = strip_tags($_REQUEST["agent_type_id"]);
    $name_short = strip_tags($_REQUEST["name_short"]);
    $name_full = strip_tags($_REQUEST["name_full"]);
	
    if ($_REQUEST["date_start"])
    {
    	$date_start = date("Y-m-d H:m:s", strtotime(strip_tags($_REQUEST["date_start"])));
    }
    else
    {
    	$date_start = date ("Y-m-d H:m:s");
    }
    
    if ($_REQUEST["date_end"])
    {
    	$date_end = date("Y-m-d H:m:s", strtotime(strip_tags($_REQUEST["date_end"])));
    }
    else
    {
    	$date_end = date ("Y-m-d H:m:s");
    }    
    
	file_put_contents  ( "agent_create_params.txt", 
		"agent_type_id '" .$agent_type_id."' name_short '" .$name_short."
		' name_full '" .$name_full."' date_start '".$date_start."' date_end '".$date_end."'");
	
	//add the agent
	$agent = new Agent();
	
	$agent->name_short = $name_short;
	$agent->name_full = $name_full;
	$agent->agent_type_id = $agent_type_id;
	$agent->date_start = $date_start;
	$agent->date_end = $date_end;
	$agent->created = date ("Y-m-d H:m:s");
	$agent->modified = date ("Y-m-d H:m:s");
	//$agent->created_by = $created_by;
	//$agent->modified_by = $modified_by;
	
	if ($_REQUEST["description"])
	{
		$agent->AgentMessage[0]->message = strip_tags($_REQUEST["description"]);
		$agent->AgentMessage[0]->subject_id = 190;
		$agent->AgentMessage[0]->rating_id = 216;
	}

		if (($agent_type_id == '12')){
			//Create People
			$titleID = strip_tags($_REQUEST["title_id"]);
			$genderID = strip_tags($_REQUEST["gender_id"]);
			$primaryRoleID = strip_tags($_REQUEST["primary_role_id"]);
			$email = strip_tags($_REQUEST["email"]);
			$telephone = strip_tags($_REQUEST["telephone"]);
			$mobile = strip_tags($_REQUEST["mobile"]);
			$imName = strip_tags($_REQUEST["im_name"]);
			$imServiceID = strip_tags($_REQUEST["im_service_id"]);
			$fax = strip_tags($_REQUEST["fax"]);
			$website = strip_tags($_REQUEST["website"]);			
			
			$agent->People->title_id = $titleID;
			$agent->People->gender_id = $genderID;
			$agent->People->primary_role_id = $primaryRoleID;
			$agent->People->email = $email;
			$agent->People->telephone = $telephone;
			$agent->People->mobile = $mobile;
			$agent->People->im_name = $imName;
			$agent->People->im_service_id = $imServiceID;
			$agent->People->fax = $fax;
			$agent->People->website = $website;
		}
 		elseif (($agent_type_id == '13'))
 		{
 		//Create Project
		    $type_id = strip_tags($_REQUEST["type_id"]);
			$status_id = strip_tags($_REQUEST["status_id"]);
			$reference_no = strip_tags($_REQUEST["reference_no"]);
			$date_application = strip_tags($_REQUEST["date_application"]);
			$date_awarded = strip_tags($_REQUEST["date_awarded"]);
			$budget_awarded = strip_tags($_REQUEST["budget_awarded"]);
			$budget_released = strip_tags($_REQUEST["budget_released"]);
			
			$agent->Projects->type_id = $type_id;
			$agent->Projects->status_id = $status_id;
			$agent->Projects->reference_no = $reference_no;
			$agent->Projects->date_application = $date_application;
			$agent->Projects->date_awarded = $date_awarded;
			$agent->Projects->budget_awarded = $budget_awarded;
			$agent->Projects->budget_released = $budget_released;
 		}
 		elseif (($agent_type_id == '14'))
	 	{
	 		//Create Nodes
	 	    $node_type_id = strip_tags($_REQUEST["node_type_id"]);
			$dev_status_id = strip_tags($_REQUEST["dev_status_id"]);
			
			$agent->Nodes->node_type_id = $node_type_id;
			$agent->Nodes->dev_status_id = $dev_status_id;
	 	}
 		elseif (($agent_type_id == '16'))
 		{
 		// Create Event Data
	    	$event_type_id = strip_tags($_REQUEST["event_type_id"]);
			$event_status_id = strip_tags($_REQUEST["event_status_id"]);
			$event_audience_id = strip_tags($_REQUEST["event_audience_id"]);
			$language_id = strip_tags($_REQUEST["language_id"]);
			
			$agent->GbifEvents->event_type_id = $event_type_id;
			$agent->GbifEvents->event_status_id = $event_status_id;
			$agent->GbifEvents->event_audience_id = $event_audience_id;
			$agent->GbifEvents->language_id = $language_id;
 		}
	 	elseif (($agent_type_id == '18'))
 		{
 		//Participant Organisations
 	 	    $purpose_id = strip_tags($_REQUEST["purpose_id"]);
			$gbif_status_id = strip_tags($_REQUEST["gbif_status_id"]);
			$phone = strip_tags($_REQUEST["phone"]);
			$fax = strip_tags($_REQUEST["fax"]);
			$email = strip_tags($_REQUEST["email"]);
			$website = strip_tags($_REQUEST["website"]);
			
			$agent->Organisations->purpose_id = $purpose_id;
			$agent->Organisations->gbif_status_id = $gbif_status_id;
			$agent->Organisations->phone = $phone;
			$agent->Organisations->fax = $fax;
			$agent->Organisations->email = $email;
			$agent->Organisations->website = $website;
			
 		}
	 	elseif (($agent_type_id == '19'))
 		{
 		//Suppliers
 	 	    $line_of_business_id = strip_tags($_REQUEST["line_of_business_id"]);
			$phone = strip_tags($_REQUEST["phone"]);
			$fax = strip_tags($_REQUEST["fax"]);
			$email = strip_tags($_REQUEST["email"]);
			$website = strip_tags($_REQUEST["website"]);
			
			$agent->Suppliers->line_of_business_id = $line_of_business_id;
			$agent->Suppliers->phone = $phone;
			$agent->Suppliers->fax = $fax;
			$agent->Suppliers->email = $email;
			$agent->Suppliers->website = $website;
 		}
 		elseif (($agent_type_id == '225'))
 		{
 		//Contracts
 	 	    $date_of_signature = strip_tags($_REQUEST["date_of_signature"]);
			$contract_status_id = strip_tags($_REQUEST["contract_status_id"]);
			$budget_final_amount = strip_tags($_REQUEST["budget_final_amount"]);
			$budget_released = strip_tags($_REQUEST["budget_released"]);
			
			$agent->Contracts->date_of_signature = $date_of_signature;
			$agent->Contracts->contract_status_id = $contract_status_id;
			$agent->Contracts->budget_final_amount = $budget_final_amount;
			$agent->Contracts->budget_released = $budget_released;
 		}
 		elseif (($agent_type_id == '28'))
 		{
 		//Resources
 	 	    $resource_link = strip_tags($_REQUEST["resource_link"]);
			
			$agent->Resources->resource_link = $resource_link;
 		}
 		 		
 		
 		
$agent->save();

$newAgentID = $agent->agent_id;

$dom = new DomDocument('1.0');
$dom->formatOutput = true;

$root = $dom->createElement( "results" );
$dom->appendChild( $root );
		$dn = $dom->createElement( "record" );
		$dn->setAttribute( 'new_agent_id', $newAgentID );
		$root->appendChild( $dn );	

header( "Content-type: text/xml");
echo $dom->saveXML();


?>